using UnityEngine;
using System.Collections;

//change speed according
public class PositionByCurveOnUpdate : CharacterAction
{
    public float forwardDistance = 0;
    public float upDistance = 0;
    public float rightDistance = 0;

	//[HideInInspector]
	public AnimationCurve movementForward = new AnimationCurve();

	//[HideInInspector]
	public AnimationCurve movementUp = new AnimationCurve();

	//[HideInInspector]
	public AnimationCurve movementRight = new AnimationCurve();

	// time at 60fps frames
	public override void OnFrameUpdate (Character.FrameUpdate arg)
	{
        Character character = arg.character;
        Vector3 velocityZ = character.transformRef.forward;
        Vector3 velocityY = character.transformRef.up;
        Vector3 velocityX = character.transformRef.right;

        float time = character.curFrameTime / character.curState.duration;
        float nextTime = (character.curFrameTime + Time.fixedDeltaTime) / character.curState.duration;
        
        float deltaZ = movementForward.Evaluate(nextTime) - movementForward.Evaluate(time);
        float deltaY = movementUp.Evaluate(nextTime) - movementUp.Evaluate(time);
        float deltaX = movementRight.Evaluate(nextTime) - movementRight.Evaluate(time);

        float ratio = 1/Time.fixedDeltaTime;

        velocityZ *= deltaZ  * forwardDistance * ratio;
        velocityY *= deltaY  * upDistance * ratio;
        velocityX *= deltaX  * rightDistance * ratio;

        character.rigidbodyRef.velocity = velocityZ + velocityY + velocityX;
	}
}

